package cn.springboot.mybatis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.springboot.mybatis.model.User;
import cn.springboot.mybatis.service.RedisService;
import cn.springboot.mybatis.service.UserService;


@Controller
public class TestController {
	@Autowired
    private RedisService redisService ;
	 @Autowired
	 private UserService userService;
    //添加
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public String saveGreeting(String user,String message) {
    	
    	String usestrr="hello";
    	String message1="world";
    	redisService.set("002", message1);
    	
    	return "OK";
    	}
    
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public Object getAll() {
    	User user1=new User();
    	user1.setAge(22);
    	user1.setUsername("test");
   //    Object O=JSON.toJSON(user1);//测试redis中放json字符串
  //  	System.out.println(O.toString());
 //   	redisService.set("003", O.toString());
    	redisService.set("003", user1);//保存的是对象
        return "ok";
    }

    //查询所有key
    @RequestMapping(value = "/getKeys", method = RequestMethod.GET)
    @ResponseBody
    public Object getKeys() {
    	 User user = userService.getUserInfo();
    	 redisService.set("003", user);
        return "ok";
    }
  
    //根据key查询
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public Object get() {
    	//从缓冲中取数据
    	Object o=redisService.get("003");//从redis中取出对象
    	if(o!=null){
    		//转换成实体类
    		User user=(User)o;
    		System.out.println(user.getPassword());
    		System.out.println(user.getUsername());
    	}
       
        return "ok";
    }

    /*
    //删除
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @ResponseBody
    public void remove() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey01");
        service.remove(m.getRedisKey());
    }

    //判断key是否存在
    @RequestMapping(value = "/isKeyExists", method = RequestMethod.GET)
    @ResponseBody
    public void isKeyExists() {
        RedisModel m = new RedisModel();
        m.setRedisKey("zhangsanKey01");
        boolean flag = service.isKeyExists(m.getRedisKey());
        System.out.println("zhangsanKey01 是否存在: "+flag);
    }

    //查询当前缓存的数量
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ResponseBody
    public Object count() {
        return service.count();
    }

    //清空所有key
    @RequestMapping(value = "/empty", method = RequestMethod.GET)
    @ResponseBody
    public void empty() {
        service.empty();
    }
*/
}

